package com.project.springboot.controller;

import com.gitee.yongzhuzl.commonutil.util.ip.IpUtil;
import com.gitee.yongzhuzl.commonutil.util.request.RequestUtil;
import com.gitee.yongzhuzl.commonutil.util.result.ResponseResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * description: 测试获取客户端IP
 * @author 朱正磊
 * @date 2024-07-26 14:51:41
 */
@RestController
@RequestMapping("/ip")
public class ClientIpController {

    /**
     * description: 获取客户端IP地址方式一
     * @author 朱正磊
     * @date 2024-07-26 14:54:17
     * @return 返回响应结果对象
     */
    @GetMapping("/getClientIp1")
    public ResponseResult<String> getClientIp1() {
        HttpServletRequest request = RequestUtil.getRequest();
        String clientIp = IpUtil.findClientIp(request);
        System.out.println("clientIp = " + clientIp);
        return ResponseResult.buildSuccess(clientIp);
    }

    /**
     * description: 获取客户端IP地址方式二
     * @author 朱正磊
     * @date 2024-07-26 14:54:17
     * @return 返回响应结果对象
     */
    @GetMapping("/getClientIp2")
    public ResponseResult<String> getClientIp2() {
        HttpServletRequest request = RequestUtil.getRequest();
        String clientIp = IpUtil.getClientIp(request);
        System.out.println("clientIp = " + clientIp);
        return ResponseResult.buildSuccess(clientIp);
    }

    /**
     * description: 获取客户端IP地址方式三
     * @author 朱正磊
     * @date 2024-07-26 14:54:17
     * @return 返回响应结果对象
     */
    @GetMapping("/getClientIp3")
    public ResponseResult<String> getClientIp3() {
        HttpServletRequest request = RequestUtil.getRequest();
        String clientIp = IpUtil.queryClientIp(request);
        System.out.println("clientIp = " + clientIp);
        return ResponseResult.buildSuccess(clientIp);
    }

    /**
     * description: 获取服务器IP地址
     * @author 朱正磊
     * @date 2024-07-26 14:54:17
     * @return 返回响应结果对象
     */
    @GetMapping("/getServerIp")
    public ResponseResult<String> getServerIp() {
        String serverIp = IpUtil.getServerIp();
        System.out.println("serverIp = " + serverIp);
        return ResponseResult.buildSuccess(serverIp);
    }

}
